//
//  ContentView.swift
//  If Light
//
//  Created by 姬向阳 on 2020/5/3.
//  Copyright © 2020 SUNNYJ. All rights reserved.
//

import SwiftUI

struct ContentView: View {
    @ObservedObject var gameManager = GameManager()
    
    var body: some View {
        VStack(alignment: .center) {
            Spacer()
            Text("能否关个灯")
                .foregroundColor(.primary)
                .font(.largeTitle)
                .bold()
            Spacer()
            Text("想办法关掉这些灯\n需要注意:每一盏灯的开关会连带其附近的灯进行开关")
                .foregroundColor(.secondary)
                .font(.footnote)
                .lineLimit(3)
                .padding(.bottom, 30)
                .opacity(gameManager.gameStatus == .ready ? 1 : 0)
            
            HStack {
                Text(gameManager.timeString)
                Spacer()
                Text("Click:\(gameManager.clickTimes)")
            }
            .font(.system(size: 20))
            .opacity(gameManager.gameStatus == .ready ? 0 : 1)
            .padding(.horizontal, 50)
            .frame(height: 30)
            
            ForEach (0..<gameManager.lights.count) { row in
                HStack {
                    ForEach (0..<self.gameManager.lights[row].count) { index in
                        Circle()
                            .foregroundColor(self.gameManager.lights[row][index].status ? .yellow : .gray)
                            .opacity(self.gameManager.lights[row][index].status ? 0.8 : 0.5)
                            .shadow(color: Color.yellow.opacity(self.gameManager.lights[row][index].status ? 1 : 0), radius: 10)
                            .frame(width:88,height: 88)
                            .padding(10)
                            .onTapGesture {
                                self.gameManager.updateLightStatus(row: row, index: index)
                        }
                    }
                }
            }
            Spacer()
            if gameManager.gameStatus == .finish {
                Button(action: {
                    self.gameManager.restartGame()
                }) {
                    Text("Restart")
                        .foregroundColor(.primary)
                        .padding(.horizontal)
                        .padding(.vertical, 6)
                        .overlay(
                            RoundedRectangle(cornerRadius: 4)
                                .stroke(Color.primary)
                    )
                }
            }else{
                Text("Place holder.")
                    .padding(.horizontal)
                    .padding(.vertical, 6)
                    .opacity(0)
            }
            Spacer()
        }
    }
}
//
//struct ContentView_Previews: PreviewProvider {
//    static var previews: some View {
//        ContentView()
//    }
//}

